package com.ruoyi.process.service.impl;


import com.ruoyi.process.entity.WfModelcontent;
import com.ruoyi.process.mapper.WfModelcontentMapper;
import com.ruoyi.process.service.FlowServiceFactory;
import com.ruoyi.process.service.IFlowDefine;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

@Service
public class FlowDefineImpl extends FlowServiceFactory implements IFlowDefine {
    @Resource
    private WfModelcontentMapper wfModelcontentMapper;
    @Override
    public void deployfile(Long pfid) {
        //部署文件，前期先从数据库表中获取,先写死，后面去掉
        WfModelcontent wfModelcontent = wfModelcontentMapper.queryById(2l);
        String modelcontent = wfModelcontent.getModelcontent();
        String strname= wfModelcontent.getName();
        String strkey= wfModelcontent.getStrkey();
        System.out.println(modelcontent);


        InputStream inputStream = new ByteArrayInputStream(modelcontent.getBytes());
        BpmnModelInstance bpmnModelInstance = Bpmn.readModelFromStream(inputStream);
        Bpmn.validateModel(bpmnModelInstance);
        Deployment deployment = repositoryService.createDeployment()
                .name(strname)
                .addModelInstance(strkey + ".bpmn", bpmnModelInstance)
                .deploy();
        // 检查部署结果
        System.out.println("Deployment ID: " + deployment.getId());
        System.out.println("Deployed process definitions: " +
                repositoryService.createProcessDefinitionQuery()
                        .deploymentId(deployment.getId())
                        .list());

    }
}
